package com.rwaste.kno.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rwaste.commonutils.Msg;
import com.rwaste.kno.entity.Popularization;
import com.rwaste.kno.entity.vo.AuditKnowVo;
import com.rwaste.kno.entity.vo.PageQueryListVo;
import com.rwaste.kno.entity.vo.UpdateStateVo;
import com.rwaste.kno.service.PopularizationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rzd
 * @since 2021-09-30
 */
@CrossOrigin
@RestController
@RequestMapping("/kno/popularization")
public class PopularizationController {
    @Autowired
    PopularizationService popularizationService;

    //用户发表评论
    @PostMapping("addKnowledge")
    public Msg addKnowledge(@RequestBody Popularization popularization) {
        popularizationService.save(popularization);
        return Msg.success();
    }

    //管理员查看用户知识信息
    //分页查询用户知识信息
    @PostMapping("pageKnowledge")
    public Msg pageListtKnowledge(@RequestBody PageQueryListVo pageQueryListVo) {
        Integer page = pageQueryListVo.getPage();
        Integer limit= pageQueryListVo.getLimit();
        //创建page对象
        Page<Popularization> pageKnowledge = new Page<>(page, limit);
        //调用方法，封装底层，把分页的所有数据分装到pageTeacher对象里面
        QueryWrapper<Popularization> wrapper=new QueryWrapper();
        //多条件组合查询
        //判断条件值是否为空
        String title=pageQueryListVo.getTitle();
        Integer state=pageQueryListVo.getState();
        if(!StringUtils.isEmpty(title)){
            // 模糊查询 ，待定。。。
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(state)){
            wrapper.eq("state",state);
        }
        popularizationService.page(pageKnowledge, wrapper);
        long total = pageKnowledge.getTotal();    //总记录数
        List<Popularization> records = pageKnowledge.getRecords();  //数据list集合
        return Msg.success().add("total", total).add("rows", records);
    }


    //根据小知识id查询 小知识信息,重写sql
    @GetMapping("getKnowByid/{id}")
    public Msg getKnowledgeByid(@PathVariable String id) {
        AuditKnowVo auditKnowVo = popularizationService.getAuditById(id);
        return Msg.success().add("getknowByid", auditKnowVo);
    }

    @ApiOperation(value = "id修改状态")
    @PostMapping("updateKnowStateByid/{id}")
    public Msg updateKnowStateByid(@PathVariable String id) {
        boolean flag = popularizationService.updateKnowState(id);
        if(flag){
            return Msg.success("修改成功");
        }
        return Msg.fail();
    }

    @ApiOperation(value = "删除小知识")
    @PostMapping("deleteKnowByid/{id}")
    public Msg deleteKnowByid(@PathVariable  String id){
        boolean flag=popularizationService.removeById(id);
        if(flag){
            return Msg.success();
        }
        return Msg.fail();
    }


    //根据小知识id查询 小知识信息
//    @GetMapping("getKnowByid/{id}")
//    public Msg getKnowledgeByid(@PathVariable String id){
//        Popularization popularization = popularizationService.getById(id);
//        return Msg.success().add("getknowByid",popularization);
//    }


    //根据小知识id，对小知识进行审核，修改小知识状态
//    @PostMapping("updateKnowStateByid")
//    public Msg updateKnowStateByid(@PathVariable String id) {
//
//        boolean flag = popularizationService.updateById();
//        if(flag){
//            return Msg.success();
//        }else
//            return Msg.fail();
//    }

   // 根据小知识id，对小知识进行审核，修改小知识状态
//    @PostMapping("updateKnowStateByid")
//    public Msg updateKnowStateByid(@RequestBody Popularization popularization) {
//        String id = popularization.getId();
//        QueryWrapper<Popularization> wrapper = new QueryWrapper<>();
//        wrapper.eq("id",id);
//        popularization.setState(1);
//        boolean b = popularizationService.update(popularization,wrapper);
//        if(b){
//
//            return    Msg.success("修改成功");
//        }
//        return Msg.fail();
//    }






}

